package spring.cloud.service.task.analyzer;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.common.curator.Curator;
import spring.cloud.tasks.common.curator.CuratorInfo;
import spring.cloud.tasks.common.nodepaths.RenewalNodePaths;
import spring.cloud.tasks.common.nodepaths.TaskNodePaths;
import spring.cloud.tasks.common.task.TaskItemExecuteRenewalInfo;
import spring.cloud.tasks.common.task.TaskItemRenewalNodeInfo;

import java.util.List;

@Slf4j
@Setter
@Getter
public class TimeoutTaskAnalyzer {


    /**
     * 查找超时告警任务
     */
    public void analyzeTimeoutTask(CuratorInfo curatorInfo, Curator curator, List<TimeoutTask> timeoutTaskList, String taskId) {
        TimeoutTask timeoutTask = new TimeoutTask(curatorInfo.getNamespaceKey(), taskId);
        if (isTimeout(curator, timeoutTask, timeoutTaskList)) {
            timeoutTaskList.add(timeoutTask);
        }
    }


    /**
     * 如果配置了超时告警时间，而且running节点存在时间大于它，则告警
     */
    private boolean isTimeout(Curator curator, TimeoutTask timeoutTask, List<TimeoutTask> oldTimeoutTaskList) {
        String taskId = timeoutTask.getTaskId();
        int timeout4AlarmSeconds = getTimeout4AlarmSeconds(curator, taskId);
        if (timeout4AlarmSeconds <= 0) {
            return false;
        }

        List<TaskItemRenewalNodeInfo> taskInstanceIdAndTaskTurnIdAndTaskItemIndexList = RenewalNodePaths.getTaskItemRenewalNodeInfoList(curator, taskId);
        if (taskInstanceIdAndTaskTurnIdAndTaskItemIndexList == null || taskInstanceIdAndTaskTurnIdAndTaskItemIndexList.isEmpty()) {
            return false;
        }
        computeTimeoutItems(curator, taskId, taskInstanceIdAndTaskTurnIdAndTaskItemIndexList, timeout4AlarmSeconds, timeoutTask);
        return !timeoutTask.getTaskInstanceAndTaskItemList().isEmpty();

    }

    private int getTimeout4AlarmSeconds(Curator curator, String taskId) {
        String timeout4AlarmSecondsStr = curator.getData(TaskNodePaths.getTaskBaseInfoNodePath(taskId, "timeout4AlarmSeconds"));
        int timeout4AlarmSeconds = 0;
        if (timeout4AlarmSecondsStr != null) {
            try {
                timeout4AlarmSeconds = Integer.parseInt(timeout4AlarmSecondsStr);
            } catch (NumberFormatException e) {
                log.error(e.getMessage(), e);
            }
        }
        return timeout4AlarmSeconds;
    }

    private void computeTimeoutItems(Curator curator, String taskId, List<TaskItemRenewalNodeInfo> taskItemIndexRenewalNodeInfoList, int timeout4AlarmSeconds, TimeoutTask timeoutTask) {
        timeoutTask.setTimeout4AlarmSeconds(timeout4AlarmSeconds);
        for (TaskItemRenewalNodeInfo taskItemIndexRenewalNodeInfo : taskItemIndexRenewalNodeInfoList) {
            //分配但是没有执行器进行续期执行
            TaskItemExecuteRenewalInfo taskItemExecuteRenewalInfo = RenewalNodePaths.getTaskItemExecuteRenewalInfo(curator, taskItemIndexRenewalNodeInfo);
            if (taskItemExecuteRenewalInfo == null) {
                timeoutTask.getTaskInstanceAndTaskItemList().add(taskItemIndexRenewalNodeInfo);
            }
        }
    }


}
